package main

import (
	"database/sql"
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/lib/pq"
	"net/http"
	"strconv"
	"twcdb/middleware"
)
var db_salary *sql.DB

func sqlOpen_salary() {
	var err error
	db_salary, err = sql.Open("postgres", "port=5432 user=postgres password=123 dbname=postgres sslmode=disable")
	//port是数据库的端口号，默认是5432，如果改了，这里一定要自定义；
	//user就是你数据库的登录帐号;
	//dbname就是你在数据库里面建立的数据库的名字;
	//sslmode就是安全验证模式;

	//还可以是这种方式打开
	//db, err := sql.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full")
	checkErr_salary(err)
	println("连接成功")
}
func main() {

	//db :=InitDB()
	//defer db.Close()
	r := gin.Default()
	r.Use(middleware.CORSMiddleware())
	r.POST("/salary", func(ctx *gin.Context) {
		//opreration :=ctx.PostForm("operation")
		//month :=ctx.PostForm("month")
		//eno :=ctx.PostForm("eno")
		ssalary :=ctx.PostForm("ssalary")
		sjob :=ctx.PostForm("sjob")
		sprices :=ctx.PostForm("sprices")
		semp :=ctx.PostForm("semp")
		shou :=ctx.PostForm("shou")
		srent :=ctx.PostForm("srent")
		sot :=ctx.PostForm("sot")
		sns :=ctx.PostForm("sns")
		ssl :=ctx.PostForm("ssl")
		sl :=ctx.PostForm("sl")
		sa :=ctx.PostForm("sa")
		sother :=ctx.PostForm("sother")
		ssum :=ctx.PostForm("ssum")
		ctx.Request.ParseForm()
		for k:=range ctx.Request.PostForm{
			s1 :=k[14: 19]
			s2 :=k[30: 37]
			s3 :=k[46: 56]
			fmt.Println(s1,s2,s3)
			fmt.Println(k)
			if s1=="selec"{
				ssalary,sjob,sprices,semp,shou,srent,sot,sns,ssl,sl,sa,sother,ssum=sqlSelect_salary(s3,s2)
				fmt.Println(ssalary,sjob,sprices,semp,shou,srent,sot,sns,ssl,sl,sa,sother,ssum)
				ctx.JSON(http.StatusOK,gin.H{"ssalary":ssalary,"sjob":sjob,"sprices":sprices,"semp":semp,"shou":shou,"srent":srent,"sot":sot,"sns":sns,"ssl":ssl,"sl":sl,"sa":sa,"sother":sother,"ssum":ssum})
			}
			if s1=="delet"{
				sqlDelete_salary(s3,s2)

			}
		}

		//switch opreration {
		//case "insert":
		//	if len(eno) ==0{
		//		ctx.JSON(http.StatusUnprocessableEntity,gin.H{"mag":"编号为空"})
		//	}
		//	sqlInsert_salary(eno,month,ssalary,sjob,sprices,semp,shou,srent,sot,sns,ssl,sl,sa,sother,ssum)
		//
		//case "select":
		//	//sqlSelect_salary(month,eno)
		//	ssalary,sjob,sprices,semp,shou,srent,sot,sns,ssl,sl,sa,sother,ssum=sqlSelect_salary(month,eno)
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"编号":eno})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"月份":month})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"基本工资":ssalary})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"岗位津贴":sjob})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"物价补贴":sprices})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"职务补贴":semp})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"住房补贴":shou})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"房租":srent})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"加班费":sot})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"夜班费":sns})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"病假扣款":ssl})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"事假扣款":sl})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"旷工扣款":sa})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"其他工资":sother})
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"实发工资":ssum})
		//
		//case "update":
		//	sqlUpdate_salary(eno,month,sjob)
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"msg":"修改成功"})
		//case "delete":
		//	sqlDelete_salary(eno,month)
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"msg":"成功删除"})
		//}
		//ctx.JSON(200, gin.H{
		//	"message": "通讯完成",
		//})
		//sqlClose_salary()
	})
	panic(r.Run(":8086") )// listen and serve on 0.0.0.0:8181

}
func sqlInsert_salary(eno,month,ssalary,sjob,sprices,semp,shou,srent,sot,sns,ssl,sl,sa,sother,ssum string) {
//func sqlInsert_salary(month,eno,sjob string) {
	//插入数据
	sqlOpen_salary()
	stmt, err := db_salary.Prepare("INSERT INTO salary(eno,month,ssalary,sjob,sprices,semp,shou,srent,sot,sns,ssl,sl,sa,sother,ssum) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15) ")
	//stmt, err := db_salary.Prepare("INSERT INTO salary(eno,month,sjob) VALUES($1,$2,$3) ")

	checkErr_salary(err)

	res, err := stmt.Exec(eno,month,ssalary,sjob,sprices,semp,shou,srent,sot,sns,ssl,sl,sa,sother,ssum)
	//这里的三个参数就是对应上面的$1,$2,$3了

	checkErr_salary(err)

	affect, err := res.RowsAffected()
	checkErr_salary(err)

	fmt.Println("rows affect:", affect)
	fmt.Println("success")
}
func sqlSelect_salary(months,enos string) (ssalary,sjob,sprices,semp,shou,srent,sot,sns,ssl,sl,sa,sother,ssum string){
	//查询数据
	sqlOpen_salary()
	rows, err := db_salary.Query("SELECT  * FROM salary")
	checkErr_salary(err)

	println("-----------")
	for rows.Next() {
		var month string
		var eno string
		var ssalary int
		var sjob int
		var sprices int
		var semp int
		var shou int
		var srent int
		var sot int
		var sns int
		var ssl int
		var sl int
		var sa int
		var sother int
		var ssum int

		//if eno!=enos{
		//	if months!=month{
		err = rows.Scan(&month, &eno,&ssalary,&sjob,&sprices,&semp,&shou,&srent,&sot,&sns,&ssl,&sl,&sa,&sother,&ssum)
		//err = rows.Scan(&month, &eno,&ssalary)
			//}
		//println(len(month),len(enos),len(eno),len(months))
		//println(month,eno)
		//}
		if eno==enos{
			fmt.Println("这里执行了2")
			if month==months{
				fmt.Println("这里执行了3")
				//fmt.Println( months,enos,dat,dwj,dml,di,dab,dl,dot,injuryrate)
				str1 := strconv.Itoa(ssalary)
				str2 := strconv.Itoa(sjob)
				str3 := strconv.Itoa(sprices)
				str4 := strconv.Itoa(semp)
				str5 := strconv.Itoa(shou)
				str6 := strconv.Itoa(srent)
				str7 := strconv.Itoa(sot)
				str8 := strconv.Itoa(sns)
				str9 := strconv.Itoa(ssl)
				str10 := strconv.Itoa(sl)
				str11 := strconv.Itoa(sa)
				str12 := strconv.Itoa(sother)
				str13 := strconv.Itoa(ssum)
				return str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12,str13
			}
		}

	}
	return "0","0","0","0","0","0","0","0","0","0","0","0","0"
}
func sqlUpdate_salary(eno, month ,sjob string) {
	//更新数据
	sqlOpen_salary()
	stmt, err := db_salary.Prepare("update salary set sjob=$1 where eno=$2 and month=$3")
	checkErr_salary(err)
	num1, err := strconv.Atoi(sjob)
	res, err := stmt.Exec(num1,eno,month)
	//res, err := stmt.Exec("ficow", 1)
	checkErr_salary(err)

	affect, err := res.RowsAffected()
	checkErr_salary(err)

	fmt.Println("rows affect:", affect)
}
func sqlDelete_salary(eno,month string) {
	//删除数据
	sqlOpen_salary()
	stmt, err := db_salary.Prepare("delete from salary where eno=$1 and month=$2")
	checkErr_salary(err)
	res, err := stmt.Exec(eno,month)

	checkErr_salary(err)

	affect, err := res.RowsAffected()

	checkErr_salary(err)

	fmt.Println("rows affect:", affect)
}
func sqlClose_salary() {
	db_salary.Close()
}
func checkErr_salary(err error) {
	if err != nil {
		panic(err)
	}
}